package com.zle.services.book;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zle.common.util.DateUtil;
import com.zle.common.util.StringUtils;
import com.zle.dao.BookOwnerMapper;
import com.zle.dao.BooksMapper;
import com.zle.dao.CommonMapper;
import com.zle.entity.BookOwner;
import com.zle.entity.BookOwnerExample;
import com.zle.entity.Books;
import com.zle.entity.BooksExample;
import com.zle.entity.BooksExample.Criteria;
import com.zle.entity.User;
import com.zle.pojo.PageInfo;

@Service
public class BookService implements IBookService{
	
	@Autowired
	private BooksMapper booksMapper;
	@Autowired
	private BookOwnerMapper bookOwnerMapper;
	@Autowired
	private CommonMapper commonMapper;
	
	@Override
	public Map<String, Object> queryMyBooks(String bname,Integer page,Integer rows,User user){
		BooksExample example = new BooksExample();
		if(StringUtils.isNotEmpty(bname)){
			Criteria criteria = example.createCriteria();
			criteria.andBookNameLike("%"+bname+"%");
		}
		example.setUid(user.getId());
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPage(page);
		pageInfo.setRows(rows);
		example.setPageInfo(pageInfo);
		List<Books> books = booksMapper.selectMyBooks(example);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("rows", books);
		result.put("total", booksMapper.selectMyBooksCount(example));
		return result;
	}
	@Override
	public Map<String, Object> queryAllBooks(String bname, Integer page, Integer rows,User user) {
		BooksExample example = new BooksExample();
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotEmpty(bname)){
			criteria.andBookNameLike("%"+bname+"%");
		}
		if(user != null){
			example.setUid(user.getId());
		}
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPage(page);
		pageInfo.setRows(rows);
		example.setPageInfo(pageInfo);
		List<Books> books = booksMapper.selectAllBooks(example);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("rows", books);
		result.put("total", booksMapper.selectAllBooksCount(example));
		return result;
	}
	@Override
	public void orderBook(User user, Integer bid) {
		addBookOwner(user.getId(),bid);
	}
	@Override
	public void orderBook(User user, String bids) {
		String bid[] = bids.split(",");
		//1.先删光
		BookOwnerExample example = new BookOwnerExample();
		com.zle.entity.BookOwnerExample.Criteria criteria = example.createCriteria();
		criteria.andUidEqualTo(user.getId());
		bookOwnerMapper.deleteByExample(example);
		//2.重新添加
		for(String sid : bid){
			addBookOwner(user.getId(),Integer.parseInt(sid));
		}
	}
	@Override
	public void orderNewBook(User user, String newbookname, String url, Integer webId) {
		
		BooksExample example = new BooksExample();
		Criteria criteria = example.createCriteria();
		criteria.andBookNameEqualTo(newbookname);
		List<Books> books = booksMapper.selectByExample(example);
		Integer bid = 0;
		//如果书已经存在不做添加
		if(books.isEmpty()){
			Books booksRecord = new Books();
			booksRecord.setBookName(newbookname);
			if(webId == 1){
				booksRecord.setWebName("笔趣阁");
			}
			booksRecord.setUrl(url);
			booksRecord.setCreateTime(DateUtil.getCurrentTime());
			booksMapper.insert(booksRecord);
			//获取新增id
			bid = commonMapper.getNewId();
			
		}else{
			bid = books.get(0).getId();
		}
		if(isOrdered(user.getId(),bid)){
			addBookOwner(user.getId(),bid);
		}
		
	}
	/**
	 * 新增订阅关系
	 * @param uid
	 * @param bid
	 */
	private void addBookOwner(Integer uid,Integer bid){
		BookOwner record = new BookOwner();
		record.setBid(bid);
		record.setUid(uid);
		bookOwnerMapper.insert(record);
	}
	@Override
	public void cancelOrderBook(User user, Integer bid) {
		
		BookOwnerExample example = new BookOwnerExample();
		com.zle.entity.BookOwnerExample.Criteria criteria = example.createCriteria();
		criteria.andBidEqualTo(bid);
		criteria.andUidEqualTo(user.getId());
		bookOwnerMapper.deleteByExample(example);
	}
	
	@Override
	public boolean isOrdered(User user, Integer bid){
		return isOrdered(user.getId(),bid);
	}
	
	private boolean isOrdered(Integer uid, Integer bid){
		BookOwnerExample example = new BookOwnerExample();
		com.zle.entity.BookOwnerExample.Criteria criteria = example.createCriteria();
		criteria.andBidEqualTo(bid);
		criteria.andUidEqualTo(uid);
		return bookOwnerMapper.selectByExample(example).isEmpty();
	}
	
}
